package com.study.ktandroid.sometest

import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.study.ktandroid.R
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class KTestActivity : AppCompatActivity() {

    lateinit var dialog: AlertDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kt_android)
        dialog = MyDialog(this)
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "jump") { _, _ -> startActivity<KTest2Activity>() }
        dialog.setCancelable(false)
        dialog.setCanceledOnTouchOutside(false)

       EventBus.getDefault().register(this)

        findViewById<Button>(R.id.btn_coro_exam)
                .onClick {
                    EventBus.getDefault().post(Event("haha"))
                }
    }

    override fun onResume() {
        super.onResume()
        //dialog.show()
    }


    @Subscribe
    fun onEvent(event: Event) {
        toast(event.msg)
    }
}

class MyDialog(context: Context) : AlertDialog(context) {
    init {
        setTitle("测试")
        setMessage("wait....")
    }

    @Subscribe
    fun onEvent(event: Event) {
        setMessage("收到数据：${event.msg}")
    }
}

data class Event(var msg: String)